package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hrbxlh.dao.QuestionDao;
import com.hrbxlh.util.PubMethods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.MemberDao;
import com.hrbxlh.dao.RelStuClassDao;

@Component("relStuClassActionImpl")
@Transactional
public class RelStuClassActionImpl {
	
	@Resource(name = "relStuClassDao")
	private RelStuClassDao relStuClassDao;

	@Resource(name="questionDao")
	private QuestionDao questionDao;

	@Autowired
	@Qualifier("questionActionImpl")
	private QuestionActionImpl questionActionImpl;
	

	/**
	 * @description 添加学生与班级关联的方法，参数key与数据库column对应即可。 
	 * @param
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, String> addRelStuClass(Map<String, Object> paramMap) throws Exception {

		Map<String, String> resultMap = new HashMap<String, String>();
		boolean result = false;
		String failMsg = "";
		try {
			
			//添加
			result = relStuClassDao.addRelStuClass(paramMap);
			
			if(!result){
				failMsg = "fail";
				resultMap.put("errorMsg", failMsg);
			}
			
			resultMap.put("result", String.valueOf(result));
			
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加学生与班级关联的方法，参数key与数据库column对应即可。
	 * @param
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public List<Map<String,Object>>  getClassByStuorteaIdAndNo(Map<String, Object> paramMap) throws Exception {

		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = relStuClassDao.getClassByStuorteaIdAndNo(paramMap);

		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	/**
	 * @description获取当前教师所教的所有班级及班级的考试状态详情
	 * @param
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object>  getAllClassStateByTeaId(Map<String, Object> paramMap,HttpServletRequest request) throws Exception {

		Map<String,Object> returnMap = new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		List<Map<String,Object>> relist = new ArrayList<Map<String, Object>>();
		String endTime = null;
		try {
			//当前登陆人id
			String stuorteaId = PubMethods.loginId(request);
			int onlinequestionId = Integer.parseInt(String.valueOf(paramMap.get("onlinequestionId")));
			//通过课程id和试卷主键id查询当前试卷是否已经与班级存在关联
			relist = relStuClassDao.getreOnlineByCouIdAndOnId(paramMap);
			if(relist==null||relist.size()==0){
				List<Map<String,Object>> paramlist = new ArrayList<Map<String, Object>>();
				//说明课又新生成了一套卷子，需要将班级与新加的卷子从新建立关联，查询教师所教的班级集合
				paramMap.put("stuorteaId",stuorteaId);
				paramlist = relStuClassDao.getClassByTeaIdAndCoId(paramMap);
				paramMap.put("list",paramlist);
				//通过这个教师这个课程教的所有班级查询试卷与班级关联表中班级的状态
				List<Map<String,Object>> classList = questionDao.getClassStateByClassIdList(paramMap);
				//将班级和状态合并在一起
				List<Map<String,Object>> finalList = contailAll(paramlist,classList);
				paramMap.put("list",finalList);
				questionDao.addreClassOnlineTwo(paramMap);
			}
			paramMap.put("onlinequestionId",onlinequestionId);
			list = relStuClassDao.getAllClassStateByTeaId(paramMap);
			returnMap.put("finalList",list);
			//查询数据库中是否存在因重启服务导致定时任务没有走，考试没有结束的数据
			/*List<Map<String,Object>> getZdataList = questionDao.getZData(paramMap);
			if(getZdataList!=null&&getZdataList.size()!=0){
				//将这些脏数据考试状态全都变为考试结束
				paramMap.put("list",getZdataList);
				questionDao.updateZreClassOnline(paramMap);
				paramMap.put("classExamState",1);
				questionActionImpl.updateSecondClassState(paramMap);
				//将没有提交当前试卷的学生---自动提交试卷
				paramMap.put("list",getZdataList);
				List<Map<String,Object>> stuIdList = relStuClassDao.getStuListNoExamErro(paramMap);
				if(stuIdList!=null&&stuIdList.size()!=0){
					//将没交试卷的学生自动提交试卷（向stuanswerquestion表插入）
					paramMap.put("stuIdList",stuIdList);
					questionDao.addNoExamStuAnswerQuestion(paramMap);
					//编辑成绩表，将学生考试成绩换算值插入数据库。
					questionActionImpl.automationAddExam(stuIdList,paramMap);
				}
			}*/
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}


	/**
	 *
	 * @param paramlist 教师教的这门课存在的班级集合
	 * @param classList 教师教的这门课程存在的班级集合（包括考试状态）
	 */
	private List<Map<String,Object>> contailAll(List<Map<String,Object>> paramlist,List<Map<String,Object>> classList){
		List<Map<String,Object>> finalList = new ArrayList<>();
		for (int i=0;i<paramlist.size();i++){
			for (int j=0;j<classList.size();j++){
				if(Integer.parseInt(String.valueOf(paramlist.get(i).get("classId")))==Integer.parseInt(String.valueOf(classList.get(j).get("classId")))){
					Map<String,Object> map = new HashMap<>();
					map.put("classId",paramlist.get(i).get("classId"));
					map.put("classExamState",classList.get(j).get("classExamState"));
					map.put("courseId",paramlist.get(i).get("courseId"));
					finalList.add(map);
					break;
				}
			}
		}
		return finalList;
	}

	/**
	 * @description获取当前课程当前教师所拥有的试卷
	 * @param
	 * @return
	 * @throws Exception
	 */
	public List<Map<String,Object>>  getTestPaperByCouId(Map<String, Object> paramMap) throws Exception {

		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = relStuClassDao.getTestPaperByCouId(paramMap);

		} catch (Exception e) {
			throw e;
		}
		return list;
	}

}
